package com.zx.znav.controller;

import com.zx.znav.bean.Link;
import com.zx.znav.bean.Tag;
import com.zx.znav.response.CommonReturnType;
import com.zx.znav.service.LinkService;
import com.zx.znav.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 链接数据
 * @since 2023-07-31 16:30
 */
@Controller
@RequestMapping("/display")
public class DisPlayController {
    @Autowired
    private LinkService linkService;
    @Autowired
    private TagService tagService;

    @RequestMapping
    public String index(){
        return "display";
    }

    @RequestMapping("/getNavLinkData")
    @ResponseBody
    public Object getNavLinkData(){
       return CommonReturnType.create(linkService.groupByFirstNav());
    }

    @RequestMapping("/getHotLinkData")
    @ResponseBody
    public Object getHotLinkData(){
        List<Link> links = linkService.topLink(20);
        //转换
        Map<String, List<Object>> map = new HashMap<>();
        //柱状图x坐标名称
        map.put("xAxis", links.stream().map(Link::getTitle).collect(Collectors.toList()));
        //柱状图对应数值
        map.put("value", links.stream().map(Link::getViewCnt).collect(Collectors.toList()));
        return CommonReturnType.create(map);
    }

    @RequestMapping("/getHotTagData")
    @ResponseBody
    public Object getHotTagData(){
        List<Tag> tags = tagService.listTopTag(30);
        Map<String, List<Object>> map = new HashMap<>();
        map.put("legendData", tags.stream().map(Tag::getName).collect(Collectors.toList()));
        map.put("seriesData", tags.stream().map(e -> {
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("name", e.getName());
            objectMap.put("value", e.getLinkCnt());
            return objectMap;
        }).collect(Collectors.toList()));
        return CommonReturnType.create(map);
    }
}
